<template>
  <div class="recoveryIndex">
    <Header>
      <span> 找回密码 </span>
    </Header>
    <section>
      <div class="iphone">
        <input
          type="text"
          placeholder="请输入手机号"
          pattern="[0-9]*"
          v-model="userTel"
          autocomplete="off"
        />
      </div>
      <div class="yzm">
        <div>
          <input
            type="text"
            placeholder="请输入短信验证码"
            pattern="[0-9]*"
            v-model="msgNum"
            autocomplete="off"
          />
        </div>
        <button class="btn" @click="getMsg" :disabled="disabled">
          {{ codeMsg }}
        </button>
      </div>
      <div class="log" @click="nextStep">下一步</div>
    </section>
  </div>
</template>

<script>
import Header from "@/components/Login/Header.vue";
import { Toast } from "mint-ui";
import http from "@/common/request";
export default {
  components: {
    Header,
  },
  data() {
    return {
      disabled: false,
      userTel: "",
      rules: {
        userTel: {
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空,并且是11位数字",
        },
      },
      codeNum: 6,
      codeMsg: "获取短信验证码",
      code: "",
      msgNum: "",
    };
  },
  methods: {
    //   点击下一步
    async nextStep() {
      // 验证码错误
      console.log(this.msgNum, this.code);
      if (this.msgNum != this.code) {
        Toast("验证码错误");
        return;
      }
      // 验证码正确
      //   先查询手机号在数据库中是否存在

      if (this.msgNum == this.code) {
        // 验证码正确  发送请求
        let res = await http.$axios({
          url: "/api/selectUser",
          method: "POST",
          data: {
            phone: this.userTel,
          },
        });
        // 没找到用户
        if (!res.data.success) {
          Toast(res.data.msg);
        } else {
          this.$router.push({
            name: "Btn",
            query:{
                phone:this.userTel
            }
          });
        }
        console.log(res);
      } else {
        Toast("验证码错误");
      }
    },
    async getMsg() {
      if (!this.validate("userTel")) return;
      // 发送短信
      let res = await http.$axios({
        url: "/api/code",
        method: "POST",
        data: {
          iphone: this.userTel,
        },
      });
      if (res.data.success) {
        this.code = res.data.data;
        console.log(this.code);
      }

      // 禁用按钮
      this.disabled = true;
      // 倒计时
      let time = setInterval(() => {
        --this.codeNum;
        this.codeMsg = `重新发送${this.codeNum}`;
      }, 1000);
      // 判断什么时候停止计时器
      setTimeout(() => {
        clearInterval(time);
        this.disabled = false;
        this.codeNum = 6;
        this.codeMsg = "获取短信验证码";
      }, 6000);
    },
    validate(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        Toast(this.rules[key].msg);
        bool = false;
        return false;
      }
      return bool;
    },
  },
};
</script>

<style scoped>
.recoveryIndex {
  height: 100vh;
  width: 100vw;
  background-color: #eee;
}
.userLogin {
  height: 100vh;
  width: 100vw;
  background-color: #eee;
}
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 40px;
}
.iphone,
.yzm {
  height: 40px;
  width: 80%;
  margin-bottom: 20px;
}
.iphone input {
  width: 100%;
  height: 100%;
  outline: none;
  padding-left: 10px;
}
.yzm {
  display: flex;
  height: 40px;
}
.yzm input {
  padding-left: 10px;
  width: 170px;
  height: 40px;
  outline: none;
}
.yzm .btn {
  box-sizing: border-box;
  height: 100%;
  flex: 1;
  font-size: 15px;
  border: 1px solid red;
  border-radius: 5px;
  text-align: center;
  line-height: 40px;
  background-color: red;
  color: antiquewhite;
  font-weight: 700;
}
.log {
  width: 80%;
  height: 40px;
  background-color: rgba(225, 0, 0, 1);
  border-radius: 10px;
  text-align: center;
  line-height: 40px;
  font-size: 18px;
  color: #eee;
}
</style>